<?php
namespace app\api\controllers;

use app\api\forms\LoginForm;

class IndexController extends ControllerBase
{
    public function initialize()
    {
        parent::initialize();
    }

    public function indexAction()
    {
        $this->view->setTemplateBefore('main');
        $this->view->title = '后台首页';

        //$adminSession = $this->session->get('admin');

        $this->assets->addJs('adminassets/js/plugins/metisMenu/jquery.metisMenu.js');
        $this->assets->addJs('adminassets/js/plugins/slimscroll/jquery.slimscroll.min.js');
        $this->assets->addJs('adminassets/js/plugins/layer/layer.min.js');
        $this->assets->addJs('adminassets/js/hplus.min.js?v=4.0.0');
        $this->assets->addJs('adminassets/js/contabs.min.js');
        $this->assets->addJs('adminassets/js/plugins/pace/pace.min.js');

    }

    public function loginAction()
    {
        $form = new LoginForm();

        if ($this->request->isPost()) {
            $parameters = $this->request->getPost();
            if($form->isValid($parameters)){

                $admin = $form->getAdmin();
                $adminSession = [
                    'username'=>$admin->username,
                    'id'=>$admin->id,
                ];

                $this->session->set('admin',$adminSession);

                $this->dispatcher->forward([
                    "controller" => "index",
                    "action" => "index"
                ]);

                return;
            }
        }

        $this->view->form = $form;
    }
}

